package com.xyyl.patient.common.easemob;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.xyyl.patient.core.constant.HttpStatusEnum;

/**
 * @projectName:xyyl-patient-web
 * @ClassName: EasemobUtil
 * @Description:环信注册账号工具类
 * @author chen
 * @date 2017年8月22日
 *
 */
public class EasemobUtil {
	
	private static Logger logger = LogManager.getLogger(EasemobUtil.class); 
	
	// 请求方式
	public static enum REQUESTMETHOD {
		POST, GET
	}

	// 授权信息
	public static enum PATHTYPE {
		token, users
	}

	public static String getToken(String tokenURL, String parameter) {
		String token = "";
		try {
			URL targetUrl = new URL(tokenURL);
			HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
			httpConnection.setDoOutput(true);
			httpConnection.setRequestMethod(REQUESTMETHOD.POST.name());
			httpConnection.setRequestProperty("Content-Type", "application/json");
			httpConnection.setRequestProperty("Accept", "application/json");
			OutputStream outputStream = httpConnection.getOutputStream();
			outputStream.write(parameter.getBytes());
			outputStream.flush();
			if (httpConnection.getResponseCode() != Integer.valueOf(HttpStatusEnum.SUCCESS.getCode())) {
				logger.error("获取Token,状态码：{}", httpConnection.getResponseCode());
				//throw new RuntimeException("Failed : HTTP error code : " + httpConnection.getResponseCode());
			}
			BufferedReader responseBuffer = new BufferedReader(
					new InputStreamReader((httpConnection.getInputStream())));
			String output;
			while ((output = responseBuffer.readLine()) != null) {
				token = output;
			}
			httpConnection.disconnect();
		} catch (MalformedURLException e) {
			logger.error("获取token失败：{}",e);
		} catch (IOException e) {
			logger.error("获取token失败：{}",e);
		}
		return token;
	}

	public static String sendResult(String parameter, String token, String regURL, REQUESTMETHOD method) {
		StringBuffer resultText = new StringBuffer();
		try {
			URL targetUrl = new URL(regURL);
			HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
			httpConnection.setDoOutput(true);
			httpConnection.setRequestMethod(method.name());
			httpConnection.setRequestProperty("Content-Type", "application/json");
			httpConnection.setRequestProperty("Accept", "application/json");
			httpConnection.setRequestProperty("Authorization", "Bearer " + token);
			if (parameter != null) {
				OutputStream outputStream = httpConnection.getOutputStream();

				outputStream.write(parameter.getBytes());
				outputStream.flush();
			}

			if (httpConnection.getResponseCode() != Integer.valueOf(HttpStatusEnum.SUCCESS.getCode())) {
				logger.error("环信注册失败,状态码：{}", httpConnection.getResponseCode());
			}
			BufferedReader responseBuffer = new BufferedReader(
					new InputStreamReader((httpConnection.getInputStream())));
			String output;
			while ((output = responseBuffer.readLine()) != null) {
				resultText.append(output);
			}
			httpConnection.disconnect();
		} catch (MalformedURLException e) {
			logger.error("获取token失败：{}",e);
		} catch (IOException e) {
			logger.error("获取token失败：{}",e);
		}
		return resultText.toString();
	}

}
